package api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"lianzi_gos/internal/mods/admin/biz"
	"lianzi_gos/internal/mods/mpweixin/schema"
	"lianzi_gos/pkg/util"
	"lianzi_gos/pkg/wxwork"
)

// banner management
type Staff struct {
	StaffBIZ *biz.Staff
}

// @Tags BannerAPI
// @Security ApiKeyAuth
// @Summary Query banner list
// @Param current query int true "pagination index" default(1)
// @Param pageSize query int true "pagination size" default(10)
// @Success 200 {object} util.ResponseResult{data=[]schema.Banner}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/banners [get]
func (a *Staff) Query(c *gin.Context) {
	ctx := c.Request.Context()
	var params schema.StaffQueryParam
	if err := util.ParseQuery(c, &params); err != nil {
		util.ResError(c, err)
		return
	}

	result, err := a.StaffBIZ.Query(ctx, params)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResPage(c, result.Data, result.PageResult)
}

// @Tags BannerAPI
// @Security ApiKeyAuth
// @Summary Get banner record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult{data=schema.Banner}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/banners/{id} [get]
func (a *Staff) Get(c *gin.Context) {
	ctx := c.Request.Context()
	item, err := a.StaffBIZ.Get(ctx, c.Param("id"))
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, item)
}

// @Tags BannerAPI
// @Security ApiKeyAuth
// @Summary Create banner record
// @Param body body schema.BannerForm true "Request body"
// @Success 200 {object} util.ResponseResult{data=schema.Banner}
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/banners [post]
func (a *Staff) Create(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.StaffForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}

	result, err := a.StaffBIZ.Create(ctx, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, result)
}

// @Tags BannerAPI
// @Security ApiKeyAuth
// @Summary Update banner record by ID
// @Param id path string true "unique id"
// @Param body body schema.BannerForm true "Request body"
// @Success 200 {object} util.ResponseResult
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/banners/{id} [put]
func (a *Staff) Update(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.StaffForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}
	err := a.StaffBIZ.Update(ctx, c.Param("id"), item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

// @Tags BannerAPI
// @Security ApiKeyAuth
// @Summary Delete banner record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/banners/{id} [delete]
func (a *Staff) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	err := a.StaffBIZ.Delete(ctx, c.Param("id"))
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

func (a *Staff) Qiye(c *gin.Context) {
	_, err := wxwork.SendWorkMessage(c, "LiJiChao", "这是一条数据")
	if err != nil {
		fmt.Println(err)
	}
}
